package com.platform.tony.controller;


import com.google.code.kaptcha.Producer;
import com.platform.tony.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;

@Slf4j
@Controller
public class IndexController {

    // https://blog.csdn.net/Ying_ph/article/details/129353108

    @Autowired
    private Producer producer;

    @GetMapping("/vc.jpg")
    public void getVerifyCode(HttpServletResponse response, HttpSession session) throws IOException {
        response.setContentType("image/png");
        String code = producer.createText();
        session.setAttribute("kaptcha", code); //可以更换成 redis 实现
        BufferedImage bi = producer.createImage(code);
        ServletOutputStream os = response.getOutputStream();
        ImageIO.write(bi, "jpg", os);
    }


    @GetMapping(value = {"home", "/"})
    public String home(){

        SecurityContext context = SecurityContextHolder.getContext();
        Authentication authentication = context.getAuthentication();
        User user = (User) authentication.getPrincipal();

        return "home";
    }

    @GetMapping(value = {"login"})
    public String login(){
        return "login";
    }

    @GetMapping(value = {"logout"})
    public String logout(){
        return "login";
    }

}
